<template>
  <div>
    <el-dialog
      :title="currentOption"
      :visible.sync="dialogVisible"
      width="700px"
    >
      <div>
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-position="right"
          label-width="80px"
        >
          <el-form-item
            label="类别"
            prop="categoryValue"
            style="width: 330px; display: inline-block"
          >
            <el-input
              v-model="form.categoryValue"
              :disabled="showInput"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="导航"
            prop="navValue"
            style="width: 330px; display: inline-block"
          >
            <el-input v-model="form.navValue"></el-input>
          </el-form-item>
          <el-form-item
            label="导航路径"
            prop="navPathValue"
            style="width: 330px; display: inline-block"
          >
            <el-input v-model="form.navPathValue"></el-input>
          </el-form-item>
          <el-form-item
            label="git地址"
            prop="gitValue"
            style="width: 330px; display: inline-block"
          >
            <el-input v-model="form.gitValue"></el-input>
          </el-form-item>
          <el-form-item label="类别描述" prop="describeValue">
            <el-input
              v-model="form.describeValue"
              :disabled="showInput"
            ></el-input>
          </el-form-item>
          <el-form-item label="导航描述" prop="otcDescribeValue">
            <el-input v-model="form.otcDescribeValue"></el-input>
          </el-form-item>
          <el-form-item
            label="导航图片"
            style="width: 400px; display: inline-block"
            prop="imgValue"
          >
            <el-input v-model="form.imgValue"></el-input>
          </el-form-item>
          <el-form-item
            label="图片背景"
            style="width: 260px; display: inline-block"
          >
            <el-input v-model="form.bgColor"></el-input>
          </el-form-item>
          <el-form-item label="图片预览">
            <img
              :src="form.imgValue"
              alt=""
              width="60px"
              height="60px"
              :style="{ 'background-color': form.bgColor }"
            />
          </el-form-item>
        </el-form>
        <!-- <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-position="right"
          label-width="80px"
        >
          
        </el-form> -->
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            type === 'add' ? addNavigation('form') : updateNavigation('form')
          "
          >确 定</el-button
        >
        <el-button @click="resetForm()">重置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
interface navigationItem {
  category: string;
  name: string;
  describe: string;
  otcName: string;
  otcDescribe: string;
  path: string;
  otcPath: string;
  bgColor: string;
}
@Component
export default class navigation extends Vue {
  private currentOption: string = "新增";
  private dialogVisible: boolean = false;
  private showInput: boolean = false;
  type: "add" | "update" = "add";
  private navigationItem: navigationItem = {
    category: "", //类别
    name: "", //git地址
    describe: "", //类别描述
    otcName: "", //应用名
    otcDescribe: "", //应用描述
    path: "", //应用路径
    otcPath: "", //图片路径
    bgColor: "", //图片背景
  };
  private rules = {
    categoryValue: [{ required: true, message: "请输入类别", trigger: "blur" }],
    navValue: [{ required: true, message: "请输入导航", trigger: "blur" }],
    categoryPath: [
      { required: true, message: "请输入类别路径", trigger: "blur" },
    ],
    gitValue: [{ required: true, message: "请输入git地址", trigger: "blur" }],
    describeValue: [
      { required: true, message: "请输入类别描述", trigger: "blur" },
    ],
    otcDescribeValue: [
      { required: true, message: "请输入导航描述", trigger: "blur" },
    ],
    imgValue: [{ required: true, message: "请输入导航图片", trigger: "blur" }],
  };
  private categoryValue: string = "";
  private form = {
    categoryValue: "",
    navValue: "",
    navPathValue: "",
    imgValue: "",
    gitValue: "",
    describeValue: "",
    otcDescribeValue: "",
    bgColor: "",
    id: "",
  };
  clearFormData() {
    this.form = {
      categoryValue: "",
      navValue: "",
      navPathValue: "",
      imgValue: "",
      gitValue: "",
      describeValue: "",
      otcDescribeValue: "",
      bgColor: "",
      id: "",
    };
  }
  updateNavigation(formName: string): void {
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        this.$axios
          .post("/navigation/update", {
            status: this.$store.state.user.userInfo.status,
            category: this.form.categoryValue,
            name: this.form.gitValue,
            describe: this.form.describeValue,
            otcName: this.form.navValue,
            otcDescribe: this.form.otcDescribeValue,
            path: this.form.navPathValue,
            otcPath: this.form.imgValue,
            id: this.form.id,
          })
          .then((res) => {
            console.log(res);
            if (res.data.error === -1) {
              this.$message({
                type: "success",
                message: "更新成功",
              });
              this.$emit("searchNav");
              this.dialogVisible = false;
              this.clearFormData();
            } else {
              this.$message({
                type: "error",
                message: "更新失败",
              });
            }
          });
      }
    });
  }
  addNavigation(formName: string): void {
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        (this as any).$axios
          .post("/navigation/add", {
            status: this.$store.state.user.userInfo.status,
            category: this.form.categoryValue,
            name: this.form.gitValue,
            describe: this.form.describeValue,
            otcName: this.form.navValue,
            otcDescribe: this.form.otcDescribeValue,
            path: this.form.navPathValue,
            otcPath: this.form.imgValue,
          })
          .then((res: any) => {
            console.log(res);
            if (res.data.error === -1) {
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.$emit("searchNav");
              this.dialogVisible = false;
              this.clearFormData();
            } else {
              this.$message({
                type: "error",
                message: "添加成功",
              });
            }
          });
      }
    });
  }
  resetForm(formName: string): void {
    this.form = {
      categoryValue: "",
      navValue: "",
      navPathValue: "",
      imgValue: "",
      gitValue: "",
      describeValue: "",
      otcDescribeValue: "",
      bgColor: "",
      id: "",
    };
  }
  delNavigation(): void {
    this.$axios.post("/navigation/del", {});
  }
}
</script>

<style>
</style>